// pages/order/orderConfirm/orderConfirm.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        activityUserId: null,
        cartGoodsInfo: {},
        orderMark: '',
        cartIdList: '',
        address: null,
        isLoading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getOrderInfo()
        this.getAddress()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    onUnload: function() {
        wx.removeStorageSync('selectAddress')
    },

    getOrderInfo: function() {
        this.setData({
            cartGoodsInfo: wx.getStorageSync('zeroBuyGoodsInfo'),
            cartIdList: wx.getStorageSync('zeroBuyCartId'),
            activityUserId: wx.getStorageSync('activityUserId')
        })
    },

    getAddress() {
        const that = this
        if (wx.getStorageSync('selectAddress')) {
            let address = wx.getStorageSync('selectAddress')
            that.setData({
                address: address
            })
        } else {
            let data = {}
            common.post('Address/address_list', data).then(function(res) {
                if (res.code == 204) {
                    wx.showToast({
                        title: res.error,
                        icon: 'none',
                        duration: 1000
                    })
                } else {
                    res.forEach(function(item) {
                        if (item.default_id == 2) {
                            that.setData({
                                address: item
                            })
                        }
                    })
                }
            }).catch(function(e) {})
        }
    },

    goselectAddress: function() {
        wx.navigateTo({
            url: '/pages/user/address/addressList/addressList?type=select'
        })
    },

    orderMarkInput: function(e) {
        this.setData({
            orderMark: e.detail.value
        })
    },

    payChange: function(e) {
        this.setData({
            payMethods: e.detail.value
        })
    },

    goPay: function() {
        const that = this
        if (this.data.address == null) {
            wx.showToast({
                title: '请选择收货地址',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.isLoading) {
            return
        }
        this.setData({
            isLoading: true
        })
        let data = {
            activity_user_id: that.data.activityUserId,
            address_id: that.data.address.address_id,
            total_price: that.data.cartGoodsInfo.price,
            breft: that.data.orderMark,
            pay_id: 8,
            pay_client: 1,
            client: 2,
            cart_id_list: that.data.cartIdList,
            is_goods: 6
        }
        common.post('Order/new_order', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {

                    }
                })
            } else {
                wx.showModal({
                    title: '提示',
                    content: '成功领取商品',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            wx.redirectTo({
                                url: '/pages/order/orderCenter/orderCenter?orderType=2'
                            })
                        }
                    }
                })
            }
        }).catch(function(e) {})
    }
})